package com.sp.scmdb.serviceImpl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.MapBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.MapDao;
import com.sp.scmdb.entity.Map;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.service.MapService;
import com.sp.scmdb.utils.UploadImage;

@Service("mapService")
public class MapServiceImpl implements MapService {

	private String imageFileName;

	private MapDao mapdao;
	private LogService logs;

	@Required
	public void setLogs(LogService logs) {
		this.logs = logs;
	}

	@Transactional
	public String addMap(MapBean t, UserBean user) {
		String result = "3";
		StringBuffer Hql = new StringBuffer("from Map where 1=1 ");
		Hql.append("and mapName='").append(t.getMapName()).append("'");
		List<Map> mapList = mapdao.queryHql(Hql.toString());
		//UploadImage upload = new UploadImage();
		if (CollectionUtils.isEmpty(mapList)) {
			Map m = new Map();
			InputStream in = null;
			if (StringUtils.isNotEmpty(t.getMapAddrFileName())) {
				try {
					byte buffer[] = new byte[(int)t.getMapAddr().length()]; 
					in = new FileInputStream(t.getMapAddr()); 
					in.read(buffer); 
					m.setMapAddr(buffer);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			    
				//imageFileName = new Date().getTime() + upload.getExtention(t.getMapAddrFileName());
				//upload.copy(t.getMapAddr(), t.getMapAddrFileName());
				//m.setMapAddr(imageFileName);
			}
			m.setMapName(t.getMapName());
			m.setMapWidth(t.getMapWidth());
			m.setMapHeight(t.getMapHeight());
			m.setIsUsed(Integer.parseInt(t.getIsUsed()));
			mapdao.save(m);
			if (t.getIsUsed().equals("1")) {
				Hql = new StringBuffer("from Map where 1=1 and isUsed=1 and mapName!='" + t.getMapName() + "'");
				mapList = mapdao.queryHql(Hql.toString());
				if (CollectionUtils.isNotEmpty(mapList)) {
					m = mapList.get(0);
					m.setIsUsed(0);
					mapdao.update(m);
				}
			}
			logs.addLog(user, "添加 地图：\""+t.getMapName()+"\"成功！");
			result = CommonParamter.CHECK_SUCCESS;
		} else {
			result = CommonParamter.CHECK_REPEAT;
		}
		return result;
	}

	public Map getMapById(String id){
		
		return mapdao.getById(Integer.parseInt(id));
	}
	
	@Transactional
	public String editMap(MapBean t, String id, UserBean user) {
		String result = "3";
		StringBuffer Hql = new StringBuffer("from Map where 1=1 ");
		Hql.append("and mapName='").append(t.getMapName()).append("'");
		List<Map> mapList = mapdao.queryHql(Hql.toString());
		List<Map> list = mapdao.queryHql("from Map where 1=1 and id=" + id);
		UploadImage upload = new UploadImage();
		Map m = new Map();
		if (CollectionUtils.isEmpty(mapList)) {
			m = list.get(0);
			InputStream in = null;
			if (StringUtils.isNotEmpty(t.getMapAddrFileName())) {
				try {
					byte buffer[] = new byte[(int)t.getMapAddr().length()]; 
					in = new FileInputStream(t.getMapAddr()); 
					in.read(buffer); 
					m.setMapAddr(buffer);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			    
				//imageFileName = new Date().getTime() + upload.getExtention(t.getMapAddrFileName());
				//upload.copy(t.getMapAddr(), t.getMapAddrFileName());
				//m.setMapAddr(imageFileName);
			}
			m.setId(Integer.parseInt(id));
			m.setMapName(t.getMapName());
			m.setMapWidth(t.getMapWidth());
			m.setMapHeight(t.getMapHeight());
			m.setIsUsed(Integer.parseInt(t.getIsUsed()));
			mapdao.update(m);
			logs.addLog(user, "修改 地图：\""+t.getMapName()+"\"成功！");
			result = CommonParamter.CHECK_SUCCESS;
		} else {
			m = mapList.get(0);
			if (m.getMapName().equals(t.getMapName())) {
				InputStream in = null;
				if (StringUtils.isNotEmpty(t.getMapAddrFileName())) {
					try {
						byte buffer[] = new byte[(int)t.getMapAddr().length()]; 
						in = new FileInputStream(t.getMapAddr()); 
						in.read(buffer); 
						m.setMapAddr(buffer);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				    
					//imageFileName = new Date().getTime() + upload.getExtention(t.getMapAddrFileName());
					//upload.copy(t.getMapAddr(), t.getMapAddrFileName());
					//m.setMapAddr(imageFileName);
				}
				m.setId(Integer.parseInt(id));
				m.setMapName(t.getMapName());
				m.setMapWidth(t.getMapWidth());
				m.setMapHeight(t.getMapHeight());
				m.setIsUsed(Integer.parseInt(t.getIsUsed()));
				mapdao.update(m);
				logs.addLog(user, "修改 地图：\""+t.getMapName()+"\"成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		if (t.getIsUsed().equals("1")) {
			Hql = new StringBuffer("from Map where 1=1 and isUsed=1 and mapName!='" + t.getMapName() + "'");
			mapList = mapdao.queryHql(Hql.toString());
			if (CollectionUtils.isNotEmpty(mapList)) {
				for (int i = 0; i < mapList.size(); i++) {
					m = mapList.get(i);
					m.setIsUsed(0);
					mapdao.update(m);
				}
			}
		}
		return result;
	}

	public List<Map> findMap(String id) {
		StringBuffer Hql = new StringBuffer("from Map where 1=1 ");
		Hql.append("and id=").append(id).append("");
		return mapdao.queryHql(Hql.toString());
	}

	@Transactional
	public String delMap(String[] arr, UserBean user) {
		String result = "3";
		Map map = new Map();
		String mapName = "";
		for (int i = 0; i < arr.length; i++) {
			map = this.findMap(arr[i]).get(0);
			mapName = map.getMapName();
			mapdao.delete(map);
			logs.addLog(user, "删除 地图：\"" + mapName + "\"成功！");
		}
		result = CommonParamter.CHECK_SUCCESS;
		return result;
	}

	@Transactional(readOnly = true)
	public List<Map> queryMap() {
		return mapdao.queryAll();
	}
	@Transactional(readOnly = true)
	public List<Map> queryIsUsed() {
		return mapdao.queryHql("from Map where 1=1 and isUsed='1'");
	}
	public void setMapdao(MapDao mapdao) {
		this.mapdao = mapdao;
	}
}
